//Item集合
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/model/article_model.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/pages/article/article_list.dart';
import 'package:village_android/pages/browser.dart';
import 'package:village_android/utils/http_utlis.dart';

class NewsCollectionPage extends StatefulWidget {
  var title;
  int module;
  int type;

  NewsCollectionPage({this.title, this.module, this.type});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new NewsCollection();
  }
}

class NewsCollection extends State<NewsCollectionPage> {
  List items = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  Future<void> _getPrefs() async {
    getData();
  }

  void getData() {
    HttpUtils.get("/api/article/list",
        data: {"limit": 5, "module": widget.module, "type": widget.type},
        success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
        padding: EdgeInsets.all(1),
        height: ScreenUtil().setHeight(220 * items.length),
        child: items.isEmpty
            ? Container(
            padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
            child: Center(child: Text('无数据')))
            : getListView());
  }

  Align initButton() {
    return Align(
        alignment: Alignment.center,
        child: Column(children: <Widget>[
          Padding(padding: EdgeInsets.only(top: 20)),
          SizedBox(
            height: 30.0,
            child: RaisedButton(
              child: Text("查看更多",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.white, fontSize: 14)),
              color: Colors.red,
              onPressed: () {
                MenuModel menuModel =
                    MenuModel(widget.title, widget.type, widget.module, null);
                var page = new ArticleListPage(
                  menuModel: menuModel,
                );
                Navigator.push(
                    context, new MaterialPageRoute(builder: (context) => page));
              },
              shape: StadiumBorder(side: BorderSide(color: Colors.white)),
            ),
          ),
        ]));
  }

  Widget getListView() {
    return ListView.separated(
        physics: new NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          ArticleModel articleModel = ArticleModel.fromJson(items[index]);
          return _newsCell(context, articleModel);
        },
        separatorBuilder: (context, index) =>
            Divider(height: 1.0, color: Colors.black12),
        itemCount: items.length);
  }

  //独立Item
  Widget _newsCell(BuildContext context, ArticleModel item) {
    Widget coverWidget(url) {
      return FadeInImage.assetNetwork(
        placeholder: 'assets/empty_image.png',
        image: url,
        width: ScreenUtil().setWidth(240),
        height: ScreenUtil().setHeight(150),
        fit: BoxFit.fitWidth,
      );
    }

    return new GestureDetector(
      onTap: () {
        onItemClick(item);
      },
      child: Container(
        height: ScreenUtil().setHeight(200),
        alignment: Alignment.center,
        padding: EdgeInsets.all(3),
        margin: EdgeInsets.all(3),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            coverWidget(item.coverUrl),
            Padding(padding: EdgeInsets.only(left: 15)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Padding(padding: EdgeInsets.only(top: 15)),
                  Text(
                    item.title,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 16,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 10)),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Row(
                      children: <Widget>[
                        Text(
                          item.updateTime + " / ",
                          maxLines: 1,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.black54,
                          ),
                        ),
                        Text(
                          widget.title,
                          maxLines: 1,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.blueAccent,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
//            Icon(Icons.arrow_forward_ios),
          ],
        ),
      ),
    );
  }

  Align initTitle(text) {
    return Align(
      alignment: Alignment.centerLeft,
      child: Row(
        children: <Widget>[
          SizedBox(
            height: 22.0,
            child: RaisedButton(
              child: Text(text,
                  textAlign: TextAlign.left,
                  style: TextStyle(color: Colors.red, fontSize: 12)),
              color: Colors.white,
              onPressed: () {},
              shape: StadiumBorder(side: BorderSide(color: Colors.red)),
            ),
          ),
        ],
      ),
    );
  }

  void onItemClick(item) {
//    item = ArticleModel.fromJson(item);
    var page = new BrowserPage(
      title: item.title,
      url: item.url,
    );
    Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
  }
}

//独立Item
Widget _girdInkWell(BuildContext context, item, VoidCallback voidCallback) {
  Widget coverWidget(item) {
    return FadeInImage.assetNetwork(
      placeholder: 'assets/empty_image.png',
      image: item["coverUrl"],
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().setHeight(100),
      fit: BoxFit.cover,
    );
  }

  Widget coverUrl(item) {
    return Image.network(
      item["coverUrl"],
      width: ScreenUtil().setWidth(100),
      height: ScreenUtil().setHeight(100),
      fit: BoxFit.fill,
    );
  }

  return InkWell(
    onTap: () {
      voidCallback();
    },
    child: Column(
      children: <Widget>[
        Expanded(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(5),
            child: coverWidget(item),
          ),
        ),
        SizedBox(
          height: ScreenUtil().setHeight(10),
        ),
        Container(
          padding: EdgeInsets.all(ScreenUtil().setHeight(5)),
          child: Text(
            item["title"],
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: ScreenUtil().setSp(16)),
          ),
        ),
      ],
    ),
  );
}
